package com.dt.dubai.mber.service;

import com.dt.dubai.core.comm.ConstantValue;
import com.dt.dubai.core.comm.Tools;
import com.dt.dubai.core.comm.ValidatorUtils;
import com.dt.dubai.core.utile.RandomUtils;
import com.dt.dubai.mber.entity.MberCustomerOrderEntity;
import com.dt.dubai.mber.service.innate.MberCustomerOrderSerInnateAbsImp;
import com.dt.dubai.mber.service.ser.MberCustomerOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class MberCustomerOrderServiceImp extends MberCustomerOrderSerInnateAbsImp implements MberCustomerOrderService {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MberCustomerOrderEntity inEntity) {

    }

    @Override
    protected void userModEntity(MberCustomerOrderEntity inEntity, MberCustomerOrderEntity upEntity) {
        upEntity.setCustomerRecordId(inEntity.getCustomerRecordId()); // 顾客登录记录ID
        upEntity.setCustomerAddrRecordId(inEntity.getCustomerAddrRecordId()); // 顾客地址记录ID
       // upEntity.setOrderNumber(inEntity.getOrderNumber()); // 订单号  不可修改
        upEntity.setPayType(inEntity.getPayType()); // 支付方式
        upEntity.setCommodityType(inEntity.getCommodityType()); // 类型
        upEntity.setOrderStatus(inEntity.getOrderStatus()); // 订单状态
        upEntity.setLogisticsFirm(inEntity.getLogisticsFirm()); // 物流公司
        upEntity.setAfterSalesStatus(inEntity.getAfterSalesStatus()); // 售后状态
        upEntity.setLogisticsNumber(inEntity.getLogisticsNumber()); // 物流单号
        upEntity.setOrderAmount(inEntity.getOrderAmount()); // 订单金额
        upEntity.setPayOrderAmount(inEntity.getPayOrderAmount()); // 实付金额
        upEntity.setDiscountOrderAmount(inEntity.getDiscountOrderAmount()); // 优惠金额
        upEntity.setOffsetOrderAmount(inEntity.getOffsetOrderAmount()); // 抵扣金额
        upEntity.setOrderRemark(inEntity.getOrderRemark()); // 订单备注

    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，static系统升级覆盖用户修改，用户代码请写入下方*/

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void applyReturnGoods(MberCustomerOrderEntity entity, Long userId) {
        MberCustomerOrderEntity upEntity = byKeyRecordId(entity.getRecordId(), userId);
        if (null == upEntity) {
            throw new RuntimeException("E47");
        }
        mberCustomerOrderMapper.applyReturnGoods(entity.getAfterSalesStatus(),entity.getRecordId());
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deliverGoods(MberCustomerOrderEntity entity, Long userId) {
        MberCustomerOrderEntity upEntity = byKeyRecordId(entity.getRecordId(), userId);
        if (null == upEntity) {
            throw new RuntimeException("E47");
        }
        mberCustomerOrderMapper.deliverGoods(entity.getDeliverGoods(),entity.getRecordId());
    }

    //添加订单
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean savEntity(MberCustomerOrderEntity inEntity, final Long userId) {
        Tools.createEntityDate(inEntity);
        //订单编号
        String s = RandomUtils.toOrderString(inEntity.getCommodityType(), inEntity.getPayType(), inEntity.getCustomerRecordId(), 3);
        inEntity.setOrderNumber(s);
        userSavEntity(inEntity);
        ValidatorUtils.validEntity(inEntity);
        int result = mberCustomerOrderMapper.insert(inEntity);
        if (result != ConstantValue.INT_1)
            throw new RuntimeException("E79");
        return ConstantValue.TRUE;
    }
}
